<?php
include("init.php");

$action =  isset($_GET['action']) ? $_GET['action'] : "";
$art_id = isset($_GET['art_id']) ? $_GET['art_id'] : 0;
$page = isset($_POST['current']) ? $_POST['current'] : 1;
$limit = isset($_POST['limit']) ? $_POST['limit'] : 6;
$fcate_id = isset($_GET['fcate_id']) ? $_GET['fcate_id'] : 0;
$food_name = isset($_GET['food_name']) ? $_GET['food_name'] : '';
$food_id = isset($_GET['food_id']) ? $_GET['food_id'] : 0;
$region_id = isset($_GET['region_id']) ? $_GET['region_id'] : 0;
$shop_name = isset($_GET['shop_name']) ? $_GET['shop_name'] : '';
$shop_id = isset($_GET['shop_id']) ? $_GET['shop_id'] : 0;
$artcate_id=isset($_GET["artcate_id"])?$_GET["artcate_id"]:0;
$art_ids=isset($_GET["art_id"])?$_GET["art_id"]:0;



if($action == "banner")  //广告图
{
    $sql = "SELECT * FROM {$pre_}article WHERE artcate_id = 1 ORDER BY art_id DESC LIMIT 10";
    $banner_list = getAll($sql);
    echo json_encode($banner_list);
    exit;
    
}else if($action == "news_food"){
    $sql = "SELECT * FROM {$pre_}food ORDER BY food_id DESC LIMIT 24";
    $news_food = getAll($sql);
    $arr = array_chunk($news_food,8);
    echo json_encode($arr);
    exit;
}else if($action == "about_us"){
    $sql = "SELECT * FROM {$pre_}article WHERE artcate_id = 2 AND art_title LIKE '%公司简介%' LIMIT 1";
    $about = getOne($sql);
    echo json_encode($about);
    exit();
}else if($action == "home_news"){  //新闻资讯
     $sql = "SELECT * FROM {$pre_}article WHERE artcate_id = 3 ORDER BY art_id DESC LIMIT 8";
     $news_food = getAll($sql);
     echo json_encode($news_food);
     exit;
 }else if($action == "home_news_one"){  //新闻资讯
   $sql = "SELECT * FROM {$pre_}article WHERE artcate_id = 3 ORDER BY art_id DESC LIMIT 1";
   $news_food_one = getOne($sql);
   echo json_encode($news_food_one);
   exit;
}else if($action == "article_detail" && $art_id){
    $sql = "SELECT * FROM {$pre_}article WHERE art_id = $art_id";
    $article_detail = getOne($sql);
    echo json_encode($article_detail);
    exit;
}else if($action == "food_cate"){
    $sql = "SELECT * FROM {$pre_}catefood LIMIT 6";
    $cate_list = getAll($sql);
    echo json_encode($cate_list);
    exit;
}else if($action == "food_count"){

    if($fcate_id)
    {
        $where = "fcate_id = $fcate_id";
    }else if(!empty($food_name)){
        $where = "food_name LIKE '%$food_name%'";
    }else{
        $where = 1;
    }

    $sql = "SELECT COUNT(*) AS c FROM {$pre_}food WHERE $where";
    $count = getOne($sql);
    echo json_encode($count);
    exit;
}else if($action == "food_list"){

    if($fcate_id)
    {
        $where = "fcate_id = $fcate_id";
    }else if(!empty($food_name)){
        $where = "food_name LIKE '%$food_name%'";
    }else{
        $where = 1;
    }
    $start = ($page-1)*$limit;
    $sql = "SELECT * FROM {$pre_}food WHERE $where LIMIT $start,$limit";
    $food_list = getAll($sql);
    echo json_encode($food_list);
    exit;
}else if($action == "food_detail"){
    $sql = "SELECT * FROM {$pre_}food AS food LEFT JOIN {$pre_}catefood AS cate ON food.fcate_id = cate.fcate_id WHERE food.food_id = $food_id";
    $food_detail = getOne($sql);
    echo json_encode($food_detail);
    exit;
}else if($action == "region_list"){
    $sql = "SELECT * FROM {$pre_}region";
    $region_list = getAll($sql);
    echo json_encode($region_list);
    exit;
}else if($action == "shop_count"){

     if($region_id)
     {
         $where = "region_id = $region_id";
     }else if(!empty($shop_name)){
         $where = "shop_name LIKE '%$shop_name%'";
     }else{
         $where = 1;
     }

     $sql = "SELECT COUNT(*) AS c FROM {$pre_}shop WHERE $where";
     $count = getOne($sql);
     echo json_encode($count);
     exit;
}else if($action == "shop_list"){

     if($region_id)
     {
         $where = "region_id = $region_id";
     }else if(!empty($shop_name)){
         $where = "shop_name LIKE '%$shop_name%'";
     }else{
         $where = 1;
     }
     $start = ($page-1)*$limit;
     $sql = "SELECT * FROM {$pre_}shop WHERE $where LIMIT $start,$limit";
     $shop_list = getAll($sql);
     echo json_encode($shop_list);
     exit;
 }else if($action == "shop_detail"){
      $sql = "SELECT * FROM {$pre_}shop AS shop LEFT JOIN {$pre_}region AS region ON shop.region_id = region.region_id WHERE shop.shop_id = $shop_id";
      $shop_detail = getOne($sql);
      echo json_encode($shop_detail);
      exit;

 }else if($action=="new_count"){  //新闻资讯总数
    
     $sql = "SELECT COUNT(*) AS c FROM {$pre_}article WHERE artcate_id =$artcate_id ";
     $count = getOne($sql);
     echo json_encode($count);
     exit;
     

 }else if($action=="new_list"){ //新闻资讯数据
    $start = ($page-1)*$limit;
     $sql="SELECT * FROM {$pre_}article WHERE artcate_id =$artcate_id LIMIT $start , $limit";

     $article=getAll($sql);

     echo json_encode($article);

     exit();

 }else if($action=="new_detail"){

    
    $sql="SELECT * FROM {$pre_}article WHERE art_id =$art_ids";

    echo $sql;

 }else if($action=="company_list"){

     $start = ($page-1)*$limit;

     $sql="SELECT * FROM {$pre_}company LIMIT $start , $limit";
     $company=getAll($sql);
     echo json_encode($company);
     exit();

 }else if($action=="ompany_count"){

     $sql="SELECT COUNT(*) AS c FROM {$pre_}company";
     $count = getOne($sql);
     echo json_encode($count);
     exit;

 }else if($action=="footer"){
     $sql="SELECT * FROM {$pre_}link LIMIT 6";

     $link=getAll($sql);

     echo json_encode($link);

     exit();


 }



?>